# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=valgrind
pkgver=3.19.0
pkgrel=0
pkgdesc="A tool to help find memory-management problems in programs"
url="https://valgrind.org/"
# armv6 not supported upstream, broken with mips64 softfloat
arch="all !armhf !mips64 !riscv64"
license="GPL-2.0-or-later"
# it seems like busybox sed works but the configure script requires GNU sed
makedepends="sed perl bash autoconf automake libtool linux-headers"
# from README_PACKAGERS:
#   Don't strip the debug info off lib/valgrind/$platform/vgpreload*.so
#   in the installation tree.  Either Valgrind won't work at all, or it
#   will still work if you do, but will generate less helpful error
#   messages.
options="!strip"
subpackages="$pkgname-dev $pkgname-doc"
source="https://sourceware.org/pub/valgrind/valgrind-$pkgver.tar.bz2
	musl.supp
	uclibc.patch
	valgrind-3.13.0-malloc.patch
	"
builddir="$srcdir"/$pkgname-$pkgver

# Valgrind 3.19.0 added support for compiling the tests on musl.
# However, it seems they haven't tested this on many architectures.
# Disable check on architectures where it doesn't work for now.
#
# TODO: Report upstream.
case "$CARCH" in
aarch64|s390x|armv7|x86) options="$options !check"
esac

prepare() {
	default_prepare
	cd "$builddir"
	cp "$srcdir"/musl.supp .
	aclocal && autoconf && automake --add-missing
	echo '#include <linux/a.out.h>' > include/a.out.h
}

build() {
	cd "$builddir"
	# fails to build with ccache
	export CC="gcc"
	export CFLAGS="$CFLAGS -fno-stack-protector -no-pie"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--without-mpicc
	make
}

check() {
	cd "$builddir"
	make check
}

package() {
	cd "$builddir"
	make DESTDIR="$pkgdir" install

	# we have options=!strip above so we strip the /usr/bin/* manually
	if [ -z "$DEBUG" ]; then
		scanelf -Ry "$pkgdir" \
			| awk '$1=="ET_DYN"{printf("%s%c",$2,0)}' \
			| xargs -0 strip
	fi
}

sha512sums="
f720a89dc4c4989cc5714bff9efe97529f71990bcfad7a92b889ce099c4326d6da07fa4d5fbab2e9125e20f352354f6178471e49e419b613a3c82c2a1c667ab2  valgrind-3.19.0.tar.bz2
49df485f158a7f4d354b2ed0d46dcc691f0490f50913c83dabfdd513b47ef1045f140fd59f54b560df05e4f3a4aba63de7124553b396189fa3ac89c908831e45  musl.supp
d59a10db9037e120df2ee94a103402ca95a79abee9d8be63e4e1bca29c82dca775cc402a79b854ec11a2160a4d2da202c237369418e221d1925267ea2613fd5d  uclibc.patch
57086a768f3876b26b0e507bc159a73f0955f03d5af8cc30e21103e348ca67f2e58b5555a5a97f299751c6602692ad43d8346bb68a80917d740fb4d65bba9665  valgrind-3.13.0-malloc.patch
"
